package com.gpzx.zx.module.home.ui.adapter

import android.content.Context
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.gpzx.zx.R

import com.gpzx.zx.databinding.ItemOrgGroupBinding
import com.gpzx.zx.databinding.ItemOrgInfoBinding
import com.gpzx.zx.module.home.frame.model.GpHomeOrgInfo
import com.wkq.lib_base.adapter.KtBaseRvAdapter

import com.wkq.lib_base.adapter.KtDataBindingViewHolder

/**
 *
 * 作者:吴奎庆
 *
 * 时间:2020/6/8
 *
 * 用途:
 */


class GpOrgAdapter(context: Context) : KtBaseRvAdapter<GpHomeOrgInfo>(context) {

    var viewClickListener: OnAdapterViewClickListener<GpHomeOrgInfo>? = null

    fun setOnViewClickListener(listener: OnAdapterViewClickListener<GpHomeOrgInfo>) {
        viewClickListener = listener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        var dataBindingViewHolder: KtDataBindingViewHolder? = null

        when (viewType) {
            1 -> {
                var viedataBinding: ViewDataBinding =
                        DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.item_org_group, parent, false)
                dataBindingViewHolder =
                        KtDataBindingViewHolder(viedataBinding.root)
                dataBindingViewHolder.setBinding(viewBinding = viedataBinding)

            }
            2 -> {
                var viedataBinding: ViewDataBinding =
                        DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.item_org_info, parent, false)
                dataBindingViewHolder =
                        KtDataBindingViewHolder(viedataBinding.root)
                dataBindingViewHolder.setBinding(viewBinding = viedataBinding)
            }
        }

        return dataBindingViewHolder!!

    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        var bindingViewHolder = holder as KtDataBindingViewHolder
        when (getItemViewType(position)) {
            1 -> {
                var homeNewsGroupBinding = bindingViewHolder.getBinding() as ItemOrgGroupBinding
                homeNewsGroupBinding.data = getItem(position)
                homeNewsGroupBinding.group.setOnClickListener {
                    if(viewClickListener!=null)
                        viewClickListener?.onViewClick(getItemViewType(position),getItem(position))
                }
            }
            2 -> {
                var homeNewsInfoBinding = bindingViewHolder.getBinding() as ItemOrgInfoBinding
                homeNewsInfoBinding.data = getItem(position)
                if (TextUtils.isEmpty(getItem(position)?.newInfo!!.o_icon)) {
                    homeNewsInfoBinding.ivIcon.visibility = View.GONE
                } else {
                    Glide.with(mContext).load(getItem(position)?.newInfo!!.o_icon).into(homeNewsInfoBinding.ivIcon)
                    homeNewsInfoBinding.ivIcon.visibility = View.VISIBLE
                }

                homeNewsInfoBinding.root.setOnClickListener {
                    if(viewClickListener!=null)
                        viewClickListener?.onViewClick(getItemViewType(position),getItem(position))
                }
            }
        }

    }

    override fun getItemCount(): Int {
        return itemList?.size ?: 0
    }

    override fun getItemViewType(position: Int): Int {
        return getItem(position)!!.type
    }


    interface OnAdapterViewClickListener<GpHomeOrgInfo> {
        fun onViewClick(type: Int, program: GpHomeOrgInfo?)
    }

}